package com.tuanzi.loan.web.bean.system.workflow;

import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import org.primefaces.model.DualListModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.system.Role;
import com.tuanzi.loan.business.entity.system.SystemUser;
import com.tuanzi.loan.business.service.system.RoleService;
import com.tuanzi.loan.business.service.system.SystemUserService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.web.configuration.JSFBean;
import com.tuanzi.loan.workflow.service.FormAttributeService;
import com.tuanzi.loan.workflow.service.FormBindingService;
import com.tuanzi.loan.workflow.service.WorkflowManagerService;
import com.tuanzi.loan.workflow.vo.StarterResponse;
import com.tuanzi.loan.workflow.vo.StarterType;

import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.extern.slf4j.Slf4j;

/**
 * @author Ray 2018/02/07
 */
@Data
@Slf4j
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class WorkflowPermissionBean extends JSFBean {

	private static final long serialVersionUID = -2152778554108785342L;

	@Autowired
	private SystemUserService systemUserService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private WorkflowManagerService workflowManagerService;
	@Autowired
	private FormBindingService formBindingService;
	@Autowired
	private FormAttributeService formAttributeService;

	private DualListModel<StarterResponse> roleStarterDualListModel;
	private DualListModel<StarterResponse> userStarterDualListModel;

	private String procDefId;

	@PostConstruct
	private void init() {
		userStarterDualListModel = new DualListModel<StarterResponse>(Lists.newArrayList(), Lists.newArrayList());
		roleStarterDualListModel = new DualListModel<StarterResponse>(Lists.newArrayList(), Lists.newArrayList());
	}

	public void onLoadPermissionData() {
		List<StarterResponse> selectedUserStarters = Lists.newArrayList();
		List<StarterResponse> selectedRoleStarters = Lists.newArrayList();

		for (StarterResponse starterResponse : workflowManagerService.findStarterBy(procDefId)) {
			switch (starterResponse.getStarterType()) {
			case USER:
				selectedUserStarters.add(starterResponse);
				break;
			case ROLE:
				selectedRoleStarters.add(starterResponse);
				break;
			default:
				log.error("不存在对应starterType : {}", starterResponse.getStarterType());
				break;
			}
		}

		List<StarterResponse> unselectedUserStarters = structureStarterFromSystemUser(systemUserService.findAll());
		List<StarterResponse> unselectedRoleStarters = structureStarterFromRole(roleService.findAll());

		unselectedUserStarters.removeAll(selectedUserStarters);
		unselectedRoleStarters.removeAll(selectedRoleStarters);

		userStarterDualListModel = new DualListModel<StarterResponse>(unselectedUserStarters, selectedUserStarters);
		roleStarterDualListModel = new DualListModel<StarterResponse>(unselectedRoleStarters, selectedRoleStarters);

	}

	private List<StarterResponse> structureStarterFromSystemUser(List<SystemUser> systemUsers) {
		return systemUsers.stream().map(x -> {
			StarterResponse response = new StarterResponse();
			response.setStarter(x.getAccount());
			response.setProcDefId(procDefId);
			response.setStarterType(StarterType.USER);
			response.setStarterName(x.getName());
			return response;
		}).collect(Collectors.toList());
	}

	private List<StarterResponse> structureStarterFromRole(List<Role> roles) {
		return roles.stream().map(x -> {
			StarterResponse response = new StarterResponse();
			response.setStarter(x.getCode());
			response.setProcDefId(procDefId);
			response.setStarterType(StarterType.ROLE);
			response.setStarterName(x.getName());
			return response;
		}).collect(Collectors.toList());
	}

	public void onSave() {
		List<StarterResponse> unselectStarterResponses = Lists.newArrayList();
		unselectStarterResponses.addAll(userStarterDualListModel.getSource());
		unselectStarterResponses.addAll(roleStarterDualListModel.getSource());
		workflowManagerService.removeStarter(unselectStarterResponses);
		List<StarterResponse> selectedStarterResponses = Lists.newArrayList();
		selectedStarterResponses.addAll(userStarterDualListModel.getTarget());
		selectedStarterResponses.addAll(roleStarterDualListModel.getTarget());
		workflowManagerService.addStarter(selectedStarterResponses);
		addSuccessfullyMessage();
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList();
	}
}
